package com.example.prison.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.SystemLog;
import com.example.prison.service.SystemLogService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Api("系统日志")
@RestController
@RequestMapping("/api")
public class SystemLogController {

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取系统日志")
    @GetMapping("/getSystemLog")
    public Response<PageInfo<SystemLog>> getSystemLog(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                        @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        log.info("获取系统日志");
        PageInfo<SystemLog> pageInfo = systemLogService.get(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增系统日志")
    @GetMapping("/addSystemLog")
    public Response<PageInfo<SystemLog>> addSystemLog(@Parameter(description = "实现功能")@RequestParam String function) {
        log.info("新增系统日志");
        Integer result = systemLogService.add(function);
        return Response.success("新增成功！");
    }

    @ApiOperation("依据操作人获取系统日志记录")
    @GetMapping("/getSystemLogByDeviceNumber")
    public Response<PageInfo<SystemLog>> getSystemLogByDeviceNumber(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                      @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                      @Parameter(description = "操作人")@RequestParam String operator) {
        log.info("依据操作人获取系统日志记录");
        PageInfo<SystemLog> pageInfo = systemLogService.getByOperator(pageNumber, pageSize, operator);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.error("无系统日志数据！");
        }
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("依据时间获取系统日志")
    @GetMapping("/getSystemLogByTime")
    public Response<PageInfo<SystemLog>> getSystemLogByTime(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                              @Parameter(description = "开始时间")@RequestParam String beginTime,
                                                              @Parameter(description = "结束时间")@RequestParam String endTime) {
        log.info("依据时间获取系统日志");
        PageInfo<SystemLog> pageInfo = systemLogService.getByTime(pageNumber, pageSize, beginTime,endTime);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.error("无系统日志数据！");
        }
        return Response.success("获取成功！", pageInfo);
    }

}
